//
//  SSDKCheckUtil.m
//  SuperSDK
//
//  Created by wyht－ios－dev on 16/7/18.
//  Copyright © 2016年 WYHT. All rights reserved.
//

#import "SSDKCheckUtil.h"
#import "SSDKHttpUtil.h"
#import "SSDKDataKeys.h"
#import "SSDKUserManager.h"
#import "SSDKDelegateWrapper.h"
#import "SSDKStatusManager.h"

@implementation SSDKCheckUtil

+ (void) checkUserLoginSuccessState:(NSString *)sdkUid accessToken:(NSString *)aToken refreshToken:(NSString *)rToken extra:(NSMutableDictionary *)extra
{
    [SSDKUserInfoUtil postWithSdkUid:sdkUid accessToken:aToken refreshToken:rToken extra:extra httpDelegate:^(SSDKBaseResData *resData) {
        SSDKUserInfo *userInfo = [[SSDKUserInfo alloc] initWithResData:resData];
        SSDKAppUserInfo *appUserInfo = [[SSDKAppUserInfo alloc] initWithUserInfo:userInfo];
        
        if ([appUserInfo isOk]) {
            if([SSDKUserManager isLogin])
            {
                if([[[[SSDKUserManager getInstance] curUserInfo] superSDKUid] isEqualToString:[userInfo superSDKUid]])
                    [SSDKUserDelegateWrapper loginSuccess: appUserInfo];
                else
                    [SSDKUserDelegateWrapper accountSwitchSuccess:appUserInfo];
            }
            else
            {
                [SSDKUserDelegateWrapper loginSuccess: appUserInfo];
            }
            [[SSDKUserManager getInstance] setCurUserInfo:userInfo];
        }
        else {
            [self checkUserLoginFailState:appUserInfo];
        }
    }];
}

+ (void) checkUserLoginFailState:(SSDKAppUserInfo *) appUserInfo
{
    if([SSDKUserManager isLogin])
    {
        [SSDKUserDelegateWrapper accountSwitchFail:appUserInfo];
    }
    else
    {
        [SSDKUserDelegateWrapper loginFail:appUserInfo];
    }
}

+ (void) checkPaySuccessState:(SSDKAppPayInfo *)appPayInfo extraData:(NSMutableDictionary *)extraData {
    [SSDKCheckPayStatesUtil postWithSSDKUid:[appPayInfo superSDKUid] orderID:[appPayInfo orderId] extraData:extraData httpDelegate:^(SSDKBaseResData *resData){
        
        SSDKAppPayInfo *payInfo=[[SSDKAppPayInfo alloc] initWithFWPayInfo:resData];
        
        if([payInfo isOk]) {
            //应对applePay的情况，只在登录成功后返回支付消息
            if([[SSDKStatusManager getInstance] loginSuccessFlag]) {
                if ([[payInfo payStatus] isEqualToString:@"SUCCESS"]) {
                    [SSDKPayDelegateWrapper paySuccess:payInfo];
                } else {
                    [SSDKPayDelegateWrapper payWaitCheck:payInfo];
                }
            }
        } else {
            if([[SSDKStatusManager getInstance] loginSuccessFlag]) {
                [SSDKPayDelegateWrapper payWaitCheck:payInfo];
            }
        }
    }];
}

+ (void) checkPaySuccessState:(SSDKAppPayInfo *)appPayInfo extraOrderID:(NSString *)extraOrderID
{
    if(extraOrderID!=nil)
    {
        NSMutableDictionary *extraData=[[NSMutableDictionary alloc] init];
        [extraData setObject:extraOrderID forKey:EXTRA_ORDER_ID];
        [self checkPaySuccessState:appPayInfo extraData:extraData];
    }
    else
        [self checkPaySuccessState:appPayInfo extraData:nil];
    
}

@end
